package com.fyt.ZhongYaoApp.service.impl;

import com.fyt.ZhongYaoApp.mapper.UserMapper;
import com.fyt.ZhongYaoApp.pojo.User;
import com.fyt.ZhongYaoApp.pojo.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        // 根据用户名查询账号信息
        User u = userMapper.findByUserName(username);
        System.out.println(username);

        LoginUser loginUser = new LoginUser(u);
        if (u == null){
            throw new RuntimeException("不存在此用户");
        }
        // 返回UserDetails对象
        return loginUser;
    }
}
